package com.augurit.common.system.web.org;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtEditorGridSupport;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.ExtEditorGridResult;
import com.augurit.common.system.convert.AcServerConverter;
import com.augurit.common.system.service.org.IAcApplicationService;
import com.augurit.common.system.service.org.IAcServerService;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AcServerForm;
import com.augurit.common.util.ExtUtils;

/**
 * Web服务器Action类
 */
@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "ac-server.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/system/org/ac-server.jsp")
})
public class AcServerAction extends ExtEditorGridSupport<AcServerForm> {

	// Service 引用
	@Autowired
	private IAcServerService acServerService;
	
	@Autowired
	private IAcApplicationService acApplicationService;
	
	// 基本属性
	private Page<AcServerForm> page = new Page<AcServerForm>(15);
	

	/**
	 * 系统首页的默认入口方法
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 分页获取Web服务器对象列表
	 */
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		acServerService.getAll(page);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 获取所有Web服务器对象下拉列表
	 */
	public String getAcServerList() throws Exception {
		List<AcServerForm> list = acServerService.getAll();
		List<BasicCombo> comboList = AcServerConverter.convertToBasicCombo(list, true);
		this.extRenderListJson(comboList, true);
		return null;
	}

	/**
	 * 新增Web服务器对象
	 */
	public String create() throws Exception {
		List<AcServerForm> list = persistChangeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridCreateMessage(list), list, false);
		return null;
	}
	
	/**
	 * 更新Web服务器对象
	 */
	public String update() throws Exception {
		List<AcServerForm> list = persistChangeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(list), list, true);
		return null;
	}

	/**
	 * 删除Web服务器对象
	 */
	public String delete() throws Exception {
		Long[] ids = result.getDeletedIds();
		acServerService.deleteCascade(ids);
		
		//同步关联更新WEB服务器列表
		WebContextUtils.syncAcServer(acServerService, acApplicationService, getApplication());
		
		this.extRenderEditorGridResult(true, ExtEditorGridResult.getEditorGridDeleteMessage(ids));
		return null;
	}

	/**
	 * 保存新增或修改的Web服务器对象
	 */
	private List<AcServerForm> persistChangeForms() throws Exception{
		List<AcServerForm> list = result.getChangedForms(AcServerForm.class);
		acServerService.saveCascade(list);
		
		//同步关联更新WEB服务器列表
		WebContextUtils.syncAcServer(acServerService, acApplicationService, getApplication());
		
		return list;
	}
}
